﻿using System.ComponentModel;
using Castle.Core.Interceptor;

namespace TestBat.Models.Interceptors.Domain
{
    public class NotifyPropertyChangedInterceptor : Castle.Core.Interceptor.IInterceptor
    {
        private PropertyChangedEventHandler subscribers = delegate { };

        public NotifyPropertyChangedInterceptor()
        {
        }

        public void Intercept(IInvocation invocation)
        {
            if (invocation.Method.DeclaringType.Equals(typeof(INotifyPropertyChanged)))
            {
                var propertyChangedEventHandler = (PropertyChangedEventHandler)invocation.Arguments[0];
                if (invocation.Method.Name.StartsWith("add_"))
                {
                    subscribers += propertyChangedEventHandler;
                }
                else
                {
                    subscribers -= propertyChangedEventHandler;
                }
                return;
            }

            invocation.Proceed();

            if (invocation.Method.Name.StartsWith("set_"))
            {
                var propertyName = invocation.Method.Name.Substring(4);
                subscribers(invocation.InvocationTarget, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
